#!/usr/bin/env python
#################################################################################
#   Copyright (C) 2007 Daniel Kersten / Tim Kersten                             #
#   (See the file AUTHORS for details.)                                         #
#                                                                               #
#   Nexus-Ed is free software; you can redistribute it and/or modify            #
#   it under the terms of the GNU General Public License as published by        #
#   the Free Software Foundation; either version 2 of the License, or           #
#   (at your option) any later version.                                         #
#                                                                               #
#   Nexus-Ed is distributed in the hope that it will be useful,                 #
#   but WITHOUT ANY WARRANTY; without even the implied warranty of              #
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                #
#   GNU General Public License for more details.                                #
#                                                                               #
#   You should have received a copy of the GNU General Public License           #
#   along with Nexus-Ed; if not, write to the Free Software                     #
#   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA    #
#   or visit http://www.gnu.org/licenses/gpl.txt                                #
#                                                                               #
#################################################################################

try:
	import gtk
	import egg.trayicon
	TRAY_AVAILABLE = True
except:
	TRAY_AVAILABLE = False

class Menu(gtk.Menu):
	"""
	A proxy for making it easier to add icons to menu items
	"""
	def __init__(self):
		"""
		Initializes the menu
		"""
		gtk.Menu.__init__(self)
	
		self.show()

	def append(self, label, callback, icon=None, data=None):
		"""
		Appends a menu item
		"""
		if icon:
			item = gtk.MenuItem()
			hbox = gtk.HBox()
			hbox.set_spacing(5)
			item.add(hbox)
			image = gtk.Image()
			image.set_from_file(icon)
			hbox.pack_start(image, False, True)
			label = gtk.Label(label)
			label.set_alignment(0, 0)
			hbox.pack_start(label, True, True)
		else:
			item = gtk.MenuItem(label)
			self.label = item.get_child()
	
		if callback: item.connect('activate', callback, data)
		gtk.Menu.append(self, item)
		item.show_all()
		return item

	def append_item(self, item):
		"""
		Appends a menu item
		"""
		gtk.Menu.append(self, item)
		item.show_all()

	def append_menu(self, label, menu, stock_id=None):
		"""
		Appends a submenu
		"""
		if stock_id:
			item = self.append(label, None, stock_id)
			item.set_submenu(menu)
			return item
	
		item = gtk.MenuItem(label)
		item.set_submenu(menu)
		item.show()
		gtk.Menu.append(self, item)
	
		return item

	def insert_menu(self, index, label, menu):
		"""
		Inserts a menu at the specified index
		"""
		item = gtk.MenuItem(label)
		item.set_submenu(menu)
		item.show()
		gtk.Menu.insert(self, item, index)
	
		return item

	def append_separator(self):
		"""
		Adds a separator
		"""
		item = gtk.SeparatorMenuItem()
		item.show()
		gtk.Menu.append(self, item)


class TrayIcon(object):
	"""
	System tray icon
	"""
	def __init__(self, app, icon, menu=None):
		"""
		Initializes the tray icon
		"""
		self.app = app
		self.tips = gtk.Tooltips()
		self.icon = egg.trayicon.TrayIcon(self.app.title)
		self.box = gtk.EventBox()
		self.icon.add(self.box)
		image = gtk.Image()
		image.set_from_file(icon)
		self.box.add(image)
		self.menu = menu
		self.activate = None
		self.box.connect('button_press_event', self.button_pressed)
		self.icon.show_all()

	def button_pressed(self, item, event, data=None):
		"""
		Called when someone clicks on the icon
		"""
		try:
			if event.button == 1 and self.activate != None:
				self.activate()
			elif event.button == 3 and self.menu != None:
				self.menu.popup(None, None, None, event.button, event.time)
		except:
			print "tray.TrayIcon.button_pressed threw an exception."

	def set_tooltip(self, tip):
		"""
		Sets the tooltip for the tray icon
		"""
		self.tips.set_tip(self.icon, tip)